package com.mc.moring.util;


import java.text.NumberFormat;
import java.util.Locale;

public class StringUtils {


    public static String isEmptyValue(final CharSequence s){
        if (isEmpty(s)){
            return "";
        }else {
            return s.toString();
        }
    }

    /**
     * Return whether the string is null or 0-length.
     *
     * @param s The string.
     * @return {@code true}: yes<br> {@code false}: no
     */
    public static boolean isEmpty(final CharSequence s) {
        return s == null || s.length() == 0;
    }



    public static boolean containsHttpsPrefixUrl(String value){
        if (StringUtils.isEmpty(value)) {
            return false;
        }
        if (value.startsWith("http://") || value.startsWith("https://")) {
            return true;
        }else {
          return false;
        }
    }

    public static String formatNumberWithCommas(String input) {
        try {
            double number = Double.parseDouble(input);
            NumberFormat numFormat = NumberFormat.getNumberInstance(new Locale("es","CL"));
            numFormat.setGroupingUsed(true);
            numFormat.format(number);

//            String[] parts = input.split("\\.");
//
//            String formatPart =  String.valueOf(parts[0])
//                    .replaceAll("(?<=\\d)(?=(\\d{3})+(\\.\\d*|$))", ",");
//
//            String decimalPart = parts.length > 1 ? "." + parts[1] : "";
//
//            return formatPart + decimalPart;

            return numFormat.format(number);
//            return decimalFormat.format(number);
        } catch (Exception e) {
            // Handle invalid number format
            e.printStackTrace();
            return input; // Return the original string, or you can provide a default value
        }
    }

    public static String keepLastFourDigits(String phoneNumber) {
        if (phoneNumber != null && phoneNumber.length() >= 4) {
            int length = phoneNumber.length();
            String lastFourDigits = phoneNumber.substring(length - 4);
            return "****" + lastFourDigits;
        } else {
            return phoneNumber;
        }
    }

    public static String convertToPercentage(double number) {
        return String.format("%.2f%%", number * 100);
    }


}
